// pages/home/home.js
const { getHomeHttp } = require("../../../utils/http")
const { titleStr } = require("../../../utils/wares/utils")
const { getCityAddress } = require("../../../utils/http")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banner: [],
    hotDrink: [],
    swiperAid: String,
    city: '',
    newCity:''
  },
  //城市
  handleCity() {
    let city = this.data.city
    let url = city === '城市' ? `../city/city?city=定位失败` : `../city/city?city=${this.data.city}`
    wx.navigateTo({
      url,
    })
  },
  //搜索
  handleSearch() {
    wx.navigateTo({
      url: '../../wares/waresSearch/waresSearch',
    })
  },
  // 轮播
  handleswiper(e) {
    let aid = this.data.swiperAid
    wx.navigateTo({
      url: `../bannerDetail/bannerDetail?aid=${aid}`,
    })
  },
  customswiper(e) {
    let { aid } = e.detail
    this.setData({ swiperAid: aid })
  },
  //商品
  handleRecommend(e) {
    let { aid } = e.currentTarget.dataset
    wx.navigateTo({
      url: `../../wares/waresDetail/waresDetail?aid=${aid}`,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    let that = this
    let city = this.data.city
    let banners = await getHomeHttp.getDrinkBannerHttp()
    let hotDrinks = await getHomeHttp.getHotDrinkHttp()
    let hotDrink = hotDrinks.data.res
    hotDrink.forEach(item => {
      item.title = titleStr(item.title)
    })
    wx.getLocation({
      type: 'wgs84',
      success: async function (res) {
        var getAddressUrl = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + res.latitude + "," + res.longitude + "&key=S3LBZ-VBG36-HK4SP-ME3HC-MWPL7-IEBTX&get_poi=1";
        let addressRes = await getCityAddress(getAddressUrl)
        let citiesRes = addressRes.data.result.ad_info
        city = citiesRes.city
        that.setData({ city })
      },
      fail: () => { that.setData({ city: '城市' }) }
    })
    this.setData({
      banner: banners.data.res,
      hotDrink
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})